package com.util;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

public class PhoneUtil {

	
	/**
	 * 屏幕宽度、高度默认
	 */
	public static int DM_WIDTH=0;
	public static int DM_HEIGHT=0;
	
	public static int getDMWidth(Context context){
		
		if(DM_WIDTH==0){
			DisplayMetrics dm=PhoneUtil.getDisplayMetrics(context);
	    	DM_WIDTH=dm.widthPixels;
	    	DM_HEIGHT=dm.heightPixels;
		}
		return DM_WIDTH;
	}
	 public static int getDMHeight(Context context){
		
		if(DM_HEIGHT==0){
			DisplayMetrics dm=PhoneUtil.getDisplayMetrics(context);
			DM_WIDTH=dm.widthPixels;
			DM_HEIGHT=dm.heightPixels;
		}
		return DM_HEIGHT;
	}
	
	
	/**
	 * 
	 * @Description: 判断网络是否可用
	 * @param context
	 * @return
	 */
	public static boolean isNetAvaiable(Context context) {
		boolean flag = true;
		ConnectivityManager mag = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mag.getActiveNetworkInfo();
		if (info == null) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 
	 * 作者: zhuwei 2012-3-12-下午4:49:07
	 * 
	 * @Description: 读取并返回手机的IMEI数据
	 * @param
	 * @return 返回手机的IMEI数据
	 * @throws
	 */
	public String getPhoneIMEI(Context context, TelephonyManager tm) {
		String imei = tm.getDeviceId();
		return imei;
	}
	/**
	 * 
	 * @Description: 获取手机分辨率
	 * @param act
	 * @return
	 */
	public static DisplayMetrics getDisplayMetrics(Context context) {
		DisplayMetrics displaysMetrics = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
		return displaysMetrics;
	}


	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
     /**
      * 
      * @Description:TODO检查某个APP是否已经安装
      * @param context
      * @param packageName
      * @return
      * @return boolean
      * @author zhuw
      * @date 2013-5-17 上午11:07:04
      */
	public static boolean checkApkExist(Context context, String packageName) {
        if (packageName == null || "".equals(packageName))
                return false;
        try {
                ApplicationInfo info = context.getPackageManager()
                                .getApplicationInfo(packageName,
                                                PackageManager.GET_UNINSTALLED_PACKAGES);
                return true;
        } catch (NameNotFoundException e) {
                return false;
        }
}
	   /***
  * 
  * @Description:打开另外的Apk程序
  * @param ctx
  * @param packageName
  */
 public static void startApkActivity(final Context ctx, String packageName) {
     PackageManager pm = ctx.getPackageManager();
     PackageInfo pi;
     try {
         pi = pm.getPackageInfo(packageName, 0);
         Intent intent = new Intent(Intent.ACTION_MAIN, null);
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
         intent.setPackage(pi.packageName);

         List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

         ResolveInfo ri = apps.iterator().next();
         if (ri != null) {
             String className = ri.activityInfo.name;
             intent.setComponent(new ComponentName(packageName, className));
             ctx.startActivity(intent);
         }
     } catch (NameNotFoundException e) {

     }
 }
	
	
	
}
